<?php

namespace app\model;

//use support\Model;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaProducts
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_products';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($fields, $condition)
    {
        $list = Db::table('wa_products')
            ->select($fields)
            ->where($condition)
            ->get();
        if ($list){
            foreach ($list as $k){

                if ($k->associated_frame_number){  $k->associated_frame_number = json_decode($k->associated_frame_number); };
//                if ($k->other_price){  $k->other_price = json_decode($k->other_price); }
                if ($k->option_package){  $k->option_package = json_decode($k->option_package); };

                // 创建人(admin_id)
                if ($k->admin_id) {
                    $admins = WaAdmins::find(['id' => intval($k->admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $k->admin_name = $admins->nickname;
                    } else {
                        $k->admin_name = '';
                    }
                } else {
                    $k->admin_name = '';
                }
                if (intval($k->car_type) == 1) {
                    $k->car_type_name = '轿车';
                }elseif (intval($k->car_type) == 2){
                    $k->car_type_name = 'SUV';
                }elseif (intval($k->car_type) == 3){
                    $k->car_type_name = 'MPV';
                }elseif (intval($k->car_type) == 4){
                    $k->car_type_name = '跑车';
                }elseif (intval($k->car_type) == 5){
                    $k->car_type_name = '新能源';
                }else{
                    $k->car_type_name = '';
                }


                // 是否新能源 1 是 2 传统能源
                if ($k->is_nev == 1){
                    $k->is_nev_name = '新能源';
                }elseif ($k->is_nev == 2){
                    $k->is_nev_name = '传统能源';
                }else{
                    $k->is_nev_name = '';
                }

                // 车辆状况(是否是新车)  1 新车 2 二手车
                if ($k->vehicle_status == 1){
                    $k->vehicle_status_name = '新车';
                }elseif ($k->vehicle_status == 2){
                    $k->vehicle_status_name = '二手车';
                }else{
                    $k->vehicle_status_name = '';
                }


            }
        }
        return json_encode($list);
    }


    public static function created($data){
        $result = Db::table('wa_products')->insertGetId($data);
        return $result;
    }

    public static function updated($data,$where){
        $result = Db::table('wa_products')->where($where)->update($data);
        return $result;
    }


    public static function list($page, $pageNum, $fields = ['*'], $condition = [], $between = [],$conditions = []){

        $offset = ($page - 1) * $pageNum;
        if ($between) {
            $model = Db::table('wa_products')
                ->select($fields)
                ->where($condition)
                ->where($conditions)
                ->whereBetween('created_at', $between);
        } else {
            $model = Db::table('wa_products')
                ->select($fields)
                ->where($condition)
                ->where($conditions);
        }

        $count = $model->where($condition)->where($conditions)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);
        if ($list){
            foreach ($list as $k){

                if ($k->associated_frame_number){  $k->associated_frame_number = json_decode($k->associated_frame_number); };
//                if ($k->other_price){  $k->other_price = json_decode($k->other_price); }
                if ($k->option_package){  $k->option_package = json_decode($k->option_package); };

                // 创建人(admin_id)
                if ($k->admin_id) {
                    $admins = WaAdmins::find(['id' => intval($k->admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $k->admin_name = $admins->nickname;
                    } else {
                        $k->admin_name = '';
                    }
                } else {
                    $k->admin_name = '';
                }
                if (intval($k->car_type) == 1) {
                    $k->car_type_name = '轿车';
                }elseif (intval($k->car_type) == 2){
                    $k->car_type_name = 'SUV';
                }elseif (intval($k->car_type) == 3){
                    $k->car_type_name = 'MPV';
                }elseif (intval($k->car_type) == 4){
                    $k->car_type_name = '跑车';
                }elseif (intval($k->car_type) == 5){
                    $k->car_type_name = '新能源';
                }else{
                    $k->car_type_name = '';
                }

                // 0 所有 1 正常 2 过期 4删除
//                if ($k->expiration_date_end > dateTime()) {
//
//                }elseif ($k->expiration_date_end < dateTime()){
//                    WaProducts::updated(['status' => 2],['id' => $k->id]);
//                }
//                if ($k->status == 1){
//                    $k->status_name = '正常';
//                }elseif ($k->status == 2){
//                    $k->status_name = '过期';
//                }else{
//                    $k->status_name = '';
//                }

                // 是否新能源 1 是 2 传统能源
                if ($k->is_nev == 1){
                    $k->is_nev_name = '新能源';
                }elseif ($k->is_nev == 2){
                    $k->is_nev_name = '传统能源';
                }else{
                    $k->is_nev_name = '';
                }

                // 车辆状况(是否是新车)  1 新车 2 二手车
                if ($k->vehicle_status == 1){
                    $k->vehicle_status_name = '新车';
                }elseif ($k->vehicle_status == 2){
                    $k->vehicle_status_name = '二手车';
                }else{
                    $k->vehicle_status_name = '';
                }

                // 产品类型 1 预售待采购 2 库存待销售 3 部分预售单（两者都有）
                if ($k->type == 1){
                    $k->type_name = '预售待采购';
                }elseif ($k->type == 2){
                    $k->type_name = '库存待销售';
                }else{
                    $k->type_name = '部分预售单';
                }
            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }



    public static function find($condition)
    {
        $res = Db::table('wa_products')
            ->where($condition)
            ->first();
        if ($res){

            if ($res->associated_frame_number){  $res->associated_frame_number = json_decode($res->associated_frame_number); };
//            if ($res->other_price){  $res->other_price = json_decode($res->other_price); }
            if ($res->option_package){  $res->option_package = json_decode($res->option_package); };
                // 创建人(admin_id)
                if ($res->admin_id) {
                    $admins = WaAdmins::find(['id' => intval($res->admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $res->admin_name = $admins->nickname;
                    } else {
                        $res->admin_name = '';
                    }
                } else {
                    $res->admin_name = '';
                }

             // 汽车类型
            if ($res->car_type == 1) {
                $res->car_type_name = '轿车';
            }elseif (intval($res->car_type) == 2){
                $res->car_type_name = 'SUV';
            }elseif (intval($res->car_type) == 3){
                $res->car_type_name = 'MPV';
            }elseif (intval($res->car_type) == 4){
                $res->car_type_name = '跑车';
            }elseif (intval($res->car_type) == 5){
                $res->car_type_name = '新能源';
            }else{
                $res->car_type_name = '';
            }

            // 产品类型 1 预售待采购 2 库存待销售 3 部分预售单（两者都有）
            if ($res->type == 1) {
                $res->type_name = ' 预售待采购';
            }elseif ($res->type == 2){
                $res->type_name = '库存待销售';
            }else{
                $res->type_name = '';
            }


            // 供应商类型： 1车辆供应商 2上牌供应商 3 国际货运代理 4保险供应商 5 国内物流供应商
            if ($res->supplier_type == 1){
                $res->supplier_type_name = '主机厂';
            }elseif ($res->supplier_type == 2){
                $res->supplier_type_name = '授权经销商';
            }elseif ($res->supplier_type == 3){
                $res->supplier_type_name = '资源公司';
            }else{
                $res->supplier_type_name = '';
            }


            // 是否新能源 1 是 2 传统能源
            if ($res->is_nev == 1){
                $res->is_nev_name = '新能源';
            }elseif ($res->is_nev == 2){
                $res->is_nev_name = '传统能源';
            }else{
                $res->is_nev_name = '';
            }

            // 车辆状况(是否是新车)  1 新车 2 二手车
            if ($res->vehicle_status == 1){
                $res->vehicle_status_name = '新车';
            }elseif ($res->vehicle_status == 2){
                $res->vehicle_status_name = '二手车';
            }else{
                $res->vehicle_status_name = '';
            }

            // 车辆状况(是否是新车)  1 新车 2 二手车
            if ($res->vehicle_status == 1){
                $res->vehicle_status_name = '新车';
            }elseif ($res->vehicle_status == 2){
                $res->vehicle_status_name = '二手车';
            }else{
                $res->vehicle_status_name = '';
            }

        }
        return json_encode($res);
    }

}